package  com.cn.adapters;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import cc.util.android.view.CCInnerListView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.simu.towercrane.R;
import com.simu.towercrane.activity.MainActivity;
import com.simu.towercrane.activity.ProductActivity;
import com.simu.towercrane.activity.PublicGroupsActivity;
import com.simu.towercrane.fragments.FragmentErShou;
import com.simu.towercrane.fragments.GlassesFragment;
import com.simu.towercrane.fragments.ProductFragment;

public class BaseImageAndTextListAdapter extends BaseAdapter {
	private String IMG_FILD = "ImageURL";
	private int mResID;
	private List<Map<String, Object>> mList;
	private ListView mListView;
	private ImageLoader mImageLoader;
	private LayoutInflater mInflater;
	private String[] mFrom;
	private int[] mTo;

	private Context mContext;
	private View viewcontext;
	private Map<String, Object> Mmap;
	/*
	 * private int[] mInvokedIDs; private OnClickListener[] mListeners;
	 */

	private int mBackgroundResID = -1;
	private LinkedHashMap<String, SoftReference<Bitmap>> lruCache;

	public BaseImageAndTextListAdapter(Context context,
			List<Map<String, Object>> list, int resourceid, String[] from,
			int[] to) {
	
		mContext=context;
		this.mList = list;
		this.mResID = resourceid;
		this.mFrom = from;
		this.mTo = to;
		
		mImageLoader = ImageLoader.getInstance();
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		lruCache = new LinkedHashMap<String, SoftReference<Bitmap>>(20, 0.75f, true) {
			private static final long serialVersionUID = -2920787383821401845L;

			@Override
			protected boolean removeEldestEntry(
					java.util.Map.Entry<String, SoftReference<Bitmap>> eldest) {
				boolean remove = size() > 20;
				if (remove) {
					SoftReference<Bitmap> soft = eldest.getValue();
					soft.clear();
				}
				return remove;
			}
		};
	}

	public void setListView(ListView listView) {
		this.mListView = listView;
	}

	public void setItemBackgroud(int resID) {
		this.mBackgroundResID = resID;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Deprecated
	public void setImgFild(String fild) {
		this.IMG_FILD = fild;
	}



	@Override
	public View getView(int position, View convertView, ViewGroup parent) {		
		View v = null;		
		if (mListView instanceof CCInnerListView) {
			v = mInflater.inflate(mResID, parent, false);
		} else {
			if (convertView == null)
			{
				v = mInflater.inflate(mResID, parent, false);
				viewcontext=v;
			}
			else 
			{
				v = convertView;
			viewcontext=v;
			}
		}
					
		bindView(mList.get(position), v);
		if (mBackgroundResID != -1)
			v.setBackgroundResource(mBackgroundResID);
		return v;
	}

	public void invokeListener(int[] ids, OnClickListener[] listeners) {
		/*
		 * this.mInvokedIDs = ids; this.mListeners = listeners;
		 */
	}

	@SuppressWarnings("unchecked")
	private void bindView(Map<String, Object> map, View v) {
		final int length = mFrom.length;				
		for (int i = 0; i < length; i++) {			
			final View view = v.findViewById(mTo[i]);
			if (view != null) {
				final Object data = map.get(mFrom[i]);
				String dataDe=data+"";
				if (view instanceof TextView) {									
					((TextView)view).setText(dataDe);
				} else if (view instanceof ImageView) {
					if(!dataDe.equals("")){
						Log.d("img", dataDe);
					}
					ImageView imageView = (ImageView)view;
					imageView.setTag(dataDe + "");
					SoftReference<Bitmap> soft = lruCache.get(dataDe + "");
					Bitmap bitmap = soft == null ? null : soft.get();
					if (bitmap != null) {
						//imageView.setImageBitmap(bitmap);
						BitmapDrawable bt=new BitmapDrawable(bitmap);
						imageView.setBackgroundDrawable(bt);
					} else {
						mImageLoader.displayImage(dataDe + "", imageView);
					}
				} else {
					Log.e("error",v.getClass().getName() + " is not a "
							+ " view that can be bounds by this SimpleAdapter");
				}
			}
		}
	
	}	
	


}
